#include "changepasswordwindow.h"

ChangePasswordWindow::ChangePasswordWindow(const QString &username, Database *db, QWidget *parent)
    : QDialog(parent)
    , username(username)
    , db(db)
{
    setupUI();
    createConnections();
}

ChangePasswordWindow::~ChangePasswordWindow()
{
}

void ChangePasswordWindow::setupUI()
{
    setWindowTitle("修改密码");
    setFixedSize(400, 300);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 旧密码
    QLabel *oldPasswordLabel = new QLabel("旧密码:", this);
    oldPasswordEdit = new QLineEdit(this);
    oldPasswordEdit->setPlaceholderText("请输入旧密码");
    oldPasswordEdit->setEchoMode(QLineEdit::Password);
    oldPasswordEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(oldPasswordLabel);
    mainLayout->addWidget(oldPasswordEdit);

    // 新密码
    QLabel *newPasswordLabel = new QLabel("新密码:", this);
    newPasswordEdit = new QLineEdit(this);
    newPasswordEdit->setPlaceholderText("请输入新密码");
    newPasswordEdit->setEchoMode(QLineEdit::Password);
    newPasswordEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(newPasswordLabel);
    mainLayout->addWidget(newPasswordEdit);

    // 确认新密码
    QLabel *confirmPasswordLabel = new QLabel("确认新密码:", this);
    confirmPasswordEdit = new QLineEdit(this);
    confirmPasswordEdit->setPlaceholderText("请再次输入新密码");
    confirmPasswordEdit->setEchoMode(QLineEdit::Password);
    confirmPasswordEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(confirmPasswordLabel);
    mainLayout->addWidget(confirmPasswordEdit);

    // 按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(20);

    changeButton = new QPushButton("修改", this);
    cancelButton = new QPushButton("取消", this);

    changeButton->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; padding: 8px 16px; "
                              "border: none; border-radius: 4px; }"
                              "QPushButton:hover { background-color: #388E3C; }"
                              "QPushButton:pressed { background-color: #1B5E20; }");

    cancelButton->setStyleSheet("QPushButton { background-color: #F44336; color: white; padding: 8px 16px; "
                              "border: none; border-radius: 4px; }"
                              "QPushButton:hover { background-color: #D32F2F; }"
                              "QPushButton:pressed { background-color: #B71C1C; }");

    buttonLayout->addWidget(changeButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    // 设置窗口样式
    setStyleSheet("QDialog { background-color: white; }");
}

void ChangePasswordWindow::createConnections()
{
    connect(changeButton, &QPushButton::clicked, this, &ChangePasswordWindow::onChangeClicked);
    connect(cancelButton, &QPushButton::clicked, this, &ChangePasswordWindow::onCancelClicked);
}

void ChangePasswordWindow::onChangeClicked()
{
    QString oldPassword = oldPasswordEdit->text().trimmed();
    QString newPassword = newPasswordEdit->text().trimmed();
    QString confirmPassword = confirmPasswordEdit->text().trimmed();

    if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
        QMessageBox::warning(this, "警告", "所有字段都不能为空！");
        return;
    }

    if (newPassword != confirmPassword) {
        QMessageBox::warning(this, "警告", "两次输入的新密码不一致！");
        return;
    }

    if (db->changePassword(username, oldPassword, newPassword)) {
        QMessageBox::information(this, "成功", "密码修改成功！");
        accept();
    } else {
        QMessageBox::warning(this, "错误", "密码修改失败！旧密码可能不正确。");
    }
}

void ChangePasswordWindow::onCancelClicked()
{
    reject();
} 